#include "stdafx.h"
#include "Localization.h"
#include "Localize.h"
const std::string & Locatization::Localize( const std::string& sTagName ) 
{	
	BuildDictionary();
	std::map<std::string,std::string>::const_iterator it=m_mDic.find(sTagName);
	if(it==m_mDic.end()) return sTagName;
	return it->second;

}

struct  InstertDicItem
{
	InstertDicItem(std::map<std::string,std::string>& rDic)
	:m_mDic(rDic)
	{

	}
	void operator()(const LocalizeItem* pItem)
	{
		m_mDic[pItem->m_sTagName]=pItem->m_sDisplayName;
	}
private:

	std::map<std::string,std::string>&m_mDic;
};


void Locatization::BuildDictionary()
{
	if(!m_bBuildDic)
	{
		LocalizeDefManager::Instance().ForeachItem(InstertDicItem(m_mDic));
		std::vector<const CClass*> vClasses;
		GlobalClassManager::Instance().EnumerateClasses(vClasses);
		for(uint32 ii=0;ii<vClasses.size();ii++)
		{
			const CClass& rClass=*vClasses[ii];
			//add class name
			std::map<std::string,std::string>::iterator it= m_mDic.find(rClass.GetName());
			if(it==m_mDic.end())
			{
				int32 iNewID=LocalizeDefManager::Instance().GetMaxID()+1;
				LocalizeItem * pNewItem=new LocalizeItem();
				pNewItem->m_iID=iNewID;
				pNewItem->m_sDisplayName=rClass.GetName();
				pNewItem->m_sTagName=rClass.GetName();
				LocalizeDefManager::Instance().AddItem(iNewID,pNewItem);
				m_mDic[rClass.GetName()]=rClass.GetName();
			}

			//add member name
			for(uint32 kk=0;kk<rClass.MemberCount();kk++)
			{
				const CMember * pMember=rClass.MemberAt(kk);
				std::map<std::string,std::string>::iterator it= m_mDic.find(pMember->GetLocalizeTag());
				if(it==m_mDic.end())
				{
					int32 iNewID=LocalizeDefManager::Instance().GetMaxID()+1;
					LocalizeItem * pNewItem=new LocalizeItem();
					pNewItem->m_iID=iNewID;
					pNewItem->m_sDisplayName=pMember->GetName();
					pNewItem->m_sTagName=pMember->GetLocalizeTag();
					LocalizeDefManager::Instance().AddItem(iNewID,pNewItem);
					m_mDic[pMember->GetLocalizeTag()]=pMember->GetName();
				}
			}
		}
		std::vector<const EnumBase*> vEnums ;
		GlobalEnumManager::Instance().EnumerateEnums(vEnums);
		for(uint32 ii=0;ii<vEnums.size();ii++)
		{
			const EnumBase * pEnum=vEnums[ii];
			for(uint32 kk=0;kk<pEnum->GetCount();kk++)
			{
				std::map<std::string,std::string>::iterator it= m_mDic.find(pEnum->GetLocalizeTag(kk));
				if(it==m_mDic.end())
				{
					int32 iNewID=LocalizeDefManager::Instance().GetMaxID()+1;
					LocalizeItem * pNewItem=new LocalizeItem();
					pNewItem->m_iID=iNewID;
					pNewItem->m_sDisplayName=pEnum->GetLocalizeTag(kk);
					pNewItem->m_sTagName=pEnum->GetLocalizeTag(kk);
					LocalizeDefManager::Instance().AddItem(iNewID,pNewItem);
					m_mDic[pEnum->GetLocalizeTag(kk)]=pEnum->GetLocalizeTag(kk);
				}
			}
		}

	}
	m_bBuildDic=true;	
}